package cn.chi.org.controller;


import cn.chi.org.domain.Employee;
import cn.chi.org.query.EmployeeQuery;
import cn.chi.org.service.IEmployeeService;
import cn.chi.util.AjaxResult;
import cn.chi.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cyf
 */
@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /*获取所有数据*/
    @GetMapping("/list")
    public List<Employee> listAll(){
        return employeeService.queryAll();
    }
    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("网络错误");
        }
    }
    /**
     * 分页查询 + 高级查询 post
     */
    @PostMapping("/page")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }
    /**
     * 根据id删除 delete
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试");
        }
    }
    /**
     * 获取一条数据 get
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }
    /**
     * 新增或修改 post
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            employeeService.addOrUpdate(employee);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试");
        }
    }
}
